package com.glory;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取context
        ServletContext context = this.getServletContext();
        // 根据键值对获取username的值
        Object username = context.getAttribute("username");
        //设置响应头的文本内容
        resp.setContentType("text/html");
        //设置编码格式
        resp.setCharacterEncoding("utf-8");
        // 输出到页面上
        PrintWriter writer = resp.getWriter();
        writer.println(String.format("名字：%s",username));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
